<?php

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;

class Down extends HomeBase {

    public function index () {
        $data = Db::name('Movies')->find(input('param.id'));
        
        if($this->request->isPost()){
            $post = input('post.');
            $result = $this->validate(
                [
                    'code'  => $post['code'],
                ],
                [
                    'code'   => 'require|captcha',
                ],
                [
                    'code.require'   => '请输入验证码',
                    'code.captcha'   => '验证码错误',
                ]);
            if(true !== $result){
                // 验证失败 输出错误信息
                $this->error($result);
            }
            
            //+1下载
            Db::name('Movies')->where(['id'=>$post['id']])->setInc('download_num',1);
            $url = Db::name('Movies')->find($post['id'])['true_download'];
            $this->success('',$url);
        }
        
        $this->view->engine->layout(false); 
        return $this->fetch('',['data'=>$data]);
    }
    
}
